﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using Core.Mappings;
using Core.Models;
using Core.Paged;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate;
using NHibernate.Linq;
using NHibernate.Tool.hbm2ddl;

namespace Test2010
{
    [TestClass]
    public partial class NHibernateTestCasts
    {
        [TestInitialize]
        public void Setup()
        {
            BuildSessionFactory();
        }

        [TestMethod]
        public void SimpleSave()
        {
            var employee = new Employee
            {
                Email = "333@333.com",
                Name = "Raymond",
                No = "AAA009",
                Phone = "110",
                Salary = 1000
            };
            using (var session = SessionFactory.OpenSession())
            {
                var id = session.Save(employee);
                session.Flush();
                Console.WriteLine(id);
            }
        }

        [TestMethod]
        public void SimpleGet()
        {
            using (var session = SessionFactory.OpenSession())
            {
                var employee = session.Get<Employee>(1);
            }
        }

        [TestMethod]
        public void SqliteTest()
        {
            using (var sesseion = BuildSqliteFactory().OpenSession())
            {
                var employee = sesseion.Query<Employee>().ToList();
                Console.WriteLine(employee.Count);
            }
        }

        [TestMethod]
        public void SqliteSaveTest()
        {
            using (var sesseion = BuildSqliteFactory().OpenSession())
            {
                var employee = new Employee
                {
                    Email = "123@123.com",
                    Name = "raymond",
                    No = "Abc123",
                    Phone = "110",
                    Salary = 1000
                };
                var id = (int)sesseion.Save(employee);
                sesseion.Flush();
            }
        }

        [TestMethod]
        public void PagedListTest()
        {
            var order1 = new OrderInfo { PropertyName = "No", IsDesc = true };
            var order2 = new OrderInfo { PropertyName = "Salary" };
            var pagedInfo = new PagedInfo
            {
                PageIndex = 1,
                PageSize = 10,
                OrderInfos = new List<OrderInfo> { order1, order2 }
            };
            using (var session = SessionFactory.OpenSession())
            {
                var employees = session.Query<Employee>()
                    .Where(x => x.Salary > 2000)
                    .ToPagedList(pagedInfo);
                session.Flush();
            }
        }

        [TestMethod]
        public void DynamicUpdateTest()
        {
            using (var session = SessionFactory.OpenSession())
            {
                var employee = session.Get<Employee>(1);
                employee.Salary = employee.Salary + 1;
                session.Flush();
            }
        }
    }

    public partial class NHibernateTestCasts
    {
        private const string HbmPath = @"..\..\Hbms";
        private const string DbScriptFile = @"db.sql";

        protected static ISessionFactory SessionFactory;

        protected static void BuildSessionFactory(bool buildTables = false)
        {
            var config = Fluently.Configure()
                //.Database(SQLiteConfiguration.Standard.ShowSql().UsingFile(DbFile))
                .Database(
                    MsSqlConfiguration.MsSql2012.ShowSql()
                        .ConnectionString(x => x.FromConnectionStringWithKey("test2010")))
                .Mappings(m =>
                {
                    m.FluentMappings.AddFromAssemblyOf<EmployeeMap>();
                    m.FluentMappings.ExportTo(HbmPath);

                }).ExposeConfiguration(c =>
                {
                    if (buildTables)
                    {
                        var schema = new SchemaExport(c);
                        schema.SetOutputFile(DbScriptFile).Execute(true, true, false);
                    }
                }).BuildConfiguration();
            foreach (var item in config.ClassMappings)
            {
                item.DynamicUpdate = true;
            }
            SessionFactory = config.BuildSessionFactory();
        }

        protected static ISessionFactory BuildSqliteFactory()
        {
            var config = Fluently.Configure()
                .Database(SQLiteConfiguration.Standard.ShowSql().UsingFile("db.sqlite"))
                .Mappings(m =>
                {
                    m.FluentMappings.AddFromAssemblyOf<EmployeeMap>();
                    m.FluentMappings.ExportTo(HbmPath);

                }).ExposeConfiguration(c =>
                {
                    var schema = new SchemaExport(c);
                    schema.SetOutputFile("sqlite.sql").Execute(true, true, false);
                }).BuildConfiguration();
            foreach (var item in config.ClassMappings)
            {
                item.DynamicUpdate = true;
            }
            return config.BuildSessionFactory();
        }
    }
}